package com.abel.bigwater.data.kente

import com.abel.bigwater.data.BcdString
import org.junit.Ignore
import org.junit.Test
import org.slf4j.LoggerFactory
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.CompletionHandler
import java.util.concurrent.TimeUnit

@Ignore
class KenteClient {
    @Test
    fun testDat1() {
        val txt1 = """41 54 2b 44 61 74 31 3d 1c 7e cb 32 01 68 14 34 01 f4 9a 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 3f 01 7e cb 32 01 68 14 34 01 1b 9b 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 eb 0b 7e cb 32 01 68 14 34 01 58 9b 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 62 9d 7e cb 32 01 68 14 34 01 89 9b 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 34 e6 7e cb 32 01 68 14 34 01 bc 9b 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 ed d8 7e cb 32 01 68 14 34 01 20 9c 01 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 0e e7 7e cb 32 01 6f 5a 41 01 e2 2d 02 00 75 b2 ae 44 06 de 8a 3e b5 cb 45
3f 0e e8 0c 00 9f 03 00 00 fc 05 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 56 55 d0 bb ea 8f 9f
41 20 03 0c 00 00 00 8a c5 7e cb 32 01 68 14 34 01 80 9c 01 00 a3 97 ae 44 71 c9 8b 3e be 95 45
3f 16 e8 0c 00 9f 03 00 00 6d 16 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 ab d2 c9 bb 42 a0 9f
41 20 03 0c 00 00 00 c6 9b 7e cb 32 01 68 14 34 01 e4 9c 01 00 5c 2c ae 44 2a 05 8c 3e b4 1f 45
3f 2d e8 0c 00 9f 03 00 00 66 1c 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 8f 4b cd bb 15 3f 9f
41 20 03 0c 00 00 00 85 1d 7e cb 32 01 68 14 34 01 48 9d 01 00 81 2f ab 44 06 cc 8d 3e f4 a1 41
3f 44 e8 0c 00 9f 03 00 00 f6 1b 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 56 55 d0 bb e2 82 9c
41 20 03 0c 00 00 00 00 83 7e cb 32 01 68 14 34 01 ac 9d 01 00 e4 36 a9 44 a6 3f 8e 3e f5 82 3f
3f 5b e8 0c 00 9f 03 00 00 6f 11 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 ab 02 c2 bb 87 b5 9a
41 20 03 0c 00 00 00 6b 88 7e cb 32 01 68 14 34 01 b0 ad 01 00 ab d4 ac 44 b0 2d 8d 3e 01 70 43
3f 72 e8 0c 00 9f 03 00 00 a7 0e 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 1d 77 cf bb f2 03 9e
41 20 03 0c 00 00 00 e2 66 7e cb 32 01 68 14 34 01 14 ae 01 00 71 20 ae 44 18 c3 8c 3e 77 0b 45
3f 89 e8 0c 00 9f 03 00 00 9e 12 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 ab ea c5 bb ba 38 9f
41 20 03 0c 00 00 00 4e bc 7e cb 32 01 68 14 34 01 78 ae 01 00 83 6f a9 44 ac 8d 8f 3e 69 c6 3f
3f a0 e8 0c 00 9f 03 00 00 f8 06 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 ab 02 c2 bb 4b e9 9a
41 20 03 0c 00 00 00 39 23 7e cb 32 01 68 14 34 01 dc ae 01 00 a8 94 ae 44 c0 9d 8e 3e b9 b0 45
3f b6 e8 0c 00 9f 03 00 00 5c 1c 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 c8 19 a7 bb 34 79 9f
41 20 03 0c 00 00 00 49 63 7e cb 32 01 68 14 34 01 40 af 01 00 3b fd af 44 bf e1 8b 3e d0 48 47
3f ce e8 0c 00 9f 03 00 00 77 0b 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 ab 02 c2 bb 33 e7 a0"""

        val buf = ByteBuffer.allocate(4096)
        buf.put(BcdString.toByteArray(txt1)).flip()

        val ak = AsynchronousSocketChannel.open()
        val f1 = ak.connect(InetSocketAddress("localhost", 9527))
        f1[2, TimeUnit.SECONDS]

        val f2 = ak.write(buf, 2, TimeUnit.SECONDS, "test", object : CompletionHandler<Int, String> {
            override fun completed(result: Int?, attachment: String?) {
                lgr.info("sent ${result} bytes for ${attachment}")
            }

            override fun failed(exc: Throwable?, attachment: String?) {
                lgr.error("fail to sent ${attachment} caused by ${exc?.message}")
            }
        })

        Thread.sleep(20000)
        ak.shutdownOutput()
        ak.shutdownInput()
        ak.close()
    }

    @Test
    fun testFailure() {
        val txt1 = """7e cb 32 01 68 14 34 01 40 af 01 00 3b fd af 44 bf e1 8b 3e d0 48 47 3f ce e8 0c 00 9f 03 00 00
77 0b 8c 03 00 00 00 00 00 00 00 00 00 00 c4 93 ab 02 c2 bb 33 e7 a0"""

        val buf = ByteBuffer.allocate(4096)
        buf.put(BcdString.toByteArray(txt1)).flip()

        val ak = AsynchronousSocketChannel.open()
        val f1 = ak.connect(InetSocketAddress("localhost", 9527))
        f1[2, TimeUnit.SECONDS]

        val f2 = ak.write(buf, 2, TimeUnit.SECONDS, "test", object : CompletionHandler<Int, String> {
            override fun completed(result: Int?, attachment: String?) {
                lgr.info("sent ${result} bytes for ${attachment}")
            }

            override fun failed(exc: Throwable?, attachment: String?) {
                lgr.error("fail to sent ${attachment} caused by ${exc?.message}")
            }
        })

        Thread.sleep(20000)
        ak.shutdownOutput()
        ak.shutdownInput()
        ak.close()
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(KenteClient::class.java)
    }
}